//
//  WithdrawViewModel.m
//  AppleMarket
//
//  Created by yangy on 07/11/2018.
//  Copyright © 2018 szgsy. All rights reserved.
//

#import "WithdrawViewModel.h"

// model
#import "CoinAssetModel.h"

@implementation WithdrawViewModel

- (instancetype)init
{
    self = [super init];
    if(self){
        [self.sendCodeCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
            if([[x objectForKey:@"Status"] boolValue]){
                NSString *account = [PlistCommon loginModel].IsMobile? [PlistCommon loginModel].PhoneNumber : [PlistCommon loginModel].Email;
                [SVProgressHUD showInfoWithStatus:[NSString stringWithFormat:@"验证码已发送至%@%@",[PlistCommon loginModel].IsMobile? @"手机" : @"邮箱",account]];
            }else{
                [SVProgressHUD showInfoWithStatus:[x objectForKey:@"Msg"]];
            }
        } error:^(NSError * _Nullable error) {
            [SVProgressHUD showInfoWithStatus:@"网络异常"];
        }];
        
        [[self.sendCodeCommand.executing skip:1] subscribeNext:^(NSNumber * _Nullable x) {
            if([x boolValue]){
                [SVProgressHUD show];
            }
        }];
        
        [self.withdrawCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
            if([[x objectForKey:@"Status"] boolValue]){
                UIAlertController *alterVC = [UIAlertController alertControllerWithTitle:@"提现申请已经提交" message:[NSString stringWithFormat:@"提现金额为%@",self.countStr_] preferredStyle:UIAlertControllerStyleAlert];
                [alterVC addAction:[UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                    [self.withdrawSuccessSubject sendNext:nil];
                }]];
            }else{
                [SVProgressHUD showInfoWithStatus:[x objectForKey:@"Msg"]];
            }
        }];
        [[self.withdrawCommand.executing skip:1] subscribeNext:^(NSNumber * _Nullable x) {
            if([x boolValue]){
                [SVProgressHUD show];
            }
        }];
    }
    return self;
}

- (RACSubject *)addressClickSubject
{
    if(!_addressClickSubject){
        _addressClickSubject = [RACSubject subject];
    }
    return _addressClickSubject;
}
- (RACSubject *)selectAddressSubject
{
    if(!_selectAddressSubject){
        _selectAddressSubject = [RACSubject subject];
    }
    return _selectAddressSubject;
}

- (RACSubject *)sendCodeClickSubject
{
    if(!_sendCodeClickSubject){
        _sendCodeClickSubject = [RACSubject subject];
    }
    return _sendCodeClickSubject;
}

- (RACCommand *)sendCodeCommand
{
    if(!_sendCodeCommand){
        _sendCodeCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            RACTupleUnpack(NSString* codeKey,NSString* inputCode) = input;
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                NSString *account = [PlistCommon loginModel].IsMobile? [PlistCommon loginModel].PhoneNumber : [PlistCommon loginModel].Email;
                [NetRequest request_SendMsgCodeType:@"DoWithdraw" account:account code:inputCode key:codeKey completeHandler:^(id  _Nonnull responseObj) {
                    [subscriber sendNext:responseObj];
                    [subscriber sendCompleted];
                } fail:^(NSError * _Nonnull error) {
                    [subscriber sendError:error];
                }];
                return nil;
            }];
        }];
    }
    return _sendCodeCommand;
}

- (RACCommand *)withdrawCommand
{
    if(!_withdrawCommand){
        _withdrawCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            NSString *pwdStr = input;
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                [NetRequest request_DoWithdrawWithCode:self.codeStr_ googleCode:nil ceId:self.coinModel.CurrencyID addr:self.blockAddr_ amount:self.countStr_ pwd:pwdStr completeHandler:^(id  _Nonnull responseObj) {
                    [subscriber sendNext:responseObj];
                    [subscriber sendCompleted];
                } fail:^(NSError * _Nonnull error) {
                    [subscriber sendError:error];
                }];
                return nil;
            }];
        }];
    }
    return _withdrawCommand;
}

- (RACSubject *)commitClickSubject
{
    if(!_commitClickSubject){
        _commitClickSubject = [RACSubject subject];
    }
    return _commitClickSubject;
}

- (RACSubject *)withdrawSuccessSubject
{
    if(!_withdrawSuccessSubject){
        _withdrawSuccessSubject = [RACSubject subject];
    }
    return _withdrawSuccessSubject;
}

@end
